﻿using Inovout.Memberships.Models;
using Inovout.Memberships.Services;
using Inovout.Models;
using Inovout.Security.Models;
using Inovout.Services;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.EntityPropertyValidation
{
    public class EntityPropertyValidationResultService : RepositoryService<EntityPropertyValidationResult>, IEntityPropertyValidationResultService
    {
        protected EntityPropertyValidationResultRepositpry EntityPropertyValidationResultRepositpry
        {
            get { return (EntityPropertyValidationResultRepositpry)base.Repository; }
        }

        public void SaveValidationResult(EntityPropertyValidationResult entity, string propertyName)
        {
            if (entity != null)
            {
                entity.PropertyName = propertyName;
                entity.ValidatedTime = System.DateTime.Now;
            }
            base.Save(entity);
        }
        public EntityPropertyValidationResult FindByEntiry(DataObject dataObject,string type,string propertyName)
        {
          
            string id = dataObject.Id.ToString();
            EntityPropertyValidationResult result = base.Repository.Find(
                new ICriterion[]{  
                   Restrictions.Eq("EntityType", type),
                   Restrictions.Eq("EntityId", id),
                   Restrictions.Eq("PropertyName",propertyName)
                });
            if (result == null)
            {
                return null;
            }
            return result;
        }
     
    }
}
